<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/1/6
 * Time: 10:28
 */

namespace Common\Logic;


//use Lcobucci\JWT\Configuration;

use Firebase\JWT\JWT;

class VideoApiLogic
{


    public static function createLiveTelecast($uid = UID){
        $url = C('PRE_API_URL') . 'createLiveTelecast?isCenter=1';
        return self::doGet($url, $uid);
    }

    public static function closeLiveTelecast($liveName,$uid = UID){
        $url = C('PRE_API_URL') . 'closeLiveTelecast?liveBane='.$liveName;
        return self::doGet($url, $uid);
    }

    public static function getLiveTelecastList($uid = UID){
        $url = C('PRE_API_URL') . 'closeLiveTelecast';
        return self::doGet($url, $uid);
    }

    public static function getCloudPlayList($uid = UID){
        $url = C('PRE_API_URL') . 'closeLiveTelecast';
        return self::doGet($url, $uid);
    }

    protected static function doGet($url, $uid){
        $header = self::getJwtHeader($uid);
        $data =  curl_request($url, false, $header);
        return json_decode($data, true);
    }


    public static function getJwtHeader($uid){
        $token = self::getToken($uid);
        return array('Authorization:Bearer '.$token) ;
//        return array('Authorization' => 'Bearer ' . $token);
    }

    public static function getToken($uid){
        vendor('phpJWT.src.JWT');
        $key = 'ewtewrewqrcqrewttytetqt43t3g321fewfewrwwrwerw';
        $now = time();
        $exp = $now + 2 * 3600;
        $token = array(
//            'iss' => 'http://api.moboa.thebdsoft.com/userLogin',
            'sub' => $uid,
//            'iat' => $now,
//            'nbf' => $now,
            'exp' => $exp,
//            'jti' => '0d71df532354482fcf301bf7e1a505d0'
        );
        $jwt = JWT::encode($token, $key);
        return $jwt;
    }

    public static function decode(){
        vendor('phpJWT.src.JWT');
        $key = 'ewtewrewqrcqrewttytetqt43t3g321fewfewrwwrwerw';
        $jwt = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjExLCJpc3MiOiJodHRwOlwvXC9hcGkubW9ib2EudGhlYmRzb2Z0LmNvbVwvdXNlckxvZ2luIiwiaWF0IjoxNDgzNjcyNDI3LCJleHAiOjE0ODM4ODg0MjcsIm5iZiI6MTQ4MzY3MjQyNywianRpIjoiMGQ3MWRmNTMyMzU0NDgyZmNmMzAxYmY3ZTFhNTA1ZDAifQ.itYHxp4prcuPfuVRKiYRDFgtjP3BFLM-VT3Kw1f99Ys';
        return JWT::decode($jwt, $key, array('HS256'));

    }

}